cmake_minimum_required(VERSION 2.8)

if (DEFINED MakeGraphGuard)
    return()
endif()

set(MakeGraphGuard yes)

find_program(PLANTULM plantuml)
if(NOT PLANTULM)
    message(STAUS "No plantuml is found. Graph is not  generated.")
endif()

include(SBE/PackageExporter)
include(SBE/helpers/SvnHelpers)

message(STATUS "Getting Dependencies Properties...")
sbeLoadContextFile(${ContextFile})
sbeGetPackageProperties(${Name})

message(STATUS "Generating Graph...")
set(plantumlContent "@startuml")

set(legend_Provided no)
set(legend_Pinned no)
set(legend_Trunk no)
set(legend_Modified no)
set(legend_DifferentUrl no)
    
foreach(pkg ${OverallDependencies})
    set(desc "")
    if(${pkg}_IsProvided)
        set(legend_Provided yes)
        list(APPEND desc "<&briefcase>")
    endif()
    if(${pkg}_IsPinned)
        set(legend_Pinned yes)
        list(APPEND desc "<&pin>")
    endif()    
    
    sbeGetPackageLocalPath(${pkg} packagePath)
    
    svnIsLocalDirectoryModified(${packagePath} isModified modifications)
    if(isModified)
        set(legend_Modified yes)
        list(APPEND desc "<&pulse>")        
    endif()
    
    sbeGetPackageUrl(${pkg} packageUrl)
    
    if(DEFINED packageUrl)
        svnGetRepositoryForLocalDirectory(${packagePath} localUrl)
        svnIsUrlTrunk(${localUrl} isTrunk)
        if(isTrunk)
            set(legend_Trunk yes)
            list(APPEND desc "<&header>")
        else()
            if(NOT "${packageUrl}" STREQUAL "${localUrl}")
                set(legend_DifferentUrl yes)
                list(APPEND desc "<&expand-left>")
            endif()        
        endif()
    endif()

    string(REPLACE ";" " " desc "${desc}")
    if(NOT "" STREQUAL "${desc}")
        set(desc "${desc}\\n\\n")
    endif()
    
    list(APPEND plantumlContent "component \"${desc}${pkg}\\n${${pkg}_VersionText}\" as ${pkg}")
endforeach()

foreach(pkg ${OverallDependencies})
    foreach(dd ${${pkg}_DirectDependencies})
        list(APPEND plantumlContent "${pkg} --> ${dd}")
    endforeach()
endforeach()

if(legend_Provided OR legend_Pinned OR legend_Trunk OR legend_Modified OR legend_DifferentUrl)
    list(APPEND plantumlContent "legend left")
    if(legend_Provided)
        list(APPEND plantumlContent "<&briefcase> ... Provided")
    endif()
    if(legend_Pinned)
        list(APPEND plantumlContent "<&pin> ... Pinned")
    endif()    
    if(legend_Modified)
        list(APPEND plantumlContent "<&pulse> ... Package is locally modifed")
    endif()    
    if(legend_Trunk)
        list(APPEND plantumlContent "<&header> ... Package is locally on trunk")
    endif()
    if(legend_DifferentUrl)
        list(APPEND plantumlContent "<&expand-left> ... Package has locally diffrent url as in context file")
    endif()    
    list(APPEND plantumlContent "endlegend")
endif()

list(APPEND plantumlContent "@enduml")

string(REPLACE ";" "\n" fileContent "${plantumlContent}\n")
file(WRITE ${OutputDirectory}/graph.txt ${fileContent})
execute_process(COMMAND ${PLANTULM} ${OutputDirectory}/graph.txt WORKING_DIRECTORY ${OutputDirectory})

message(STATUS "Graph is generated to ${OutputDirectory}/graph.png")